package git.soulbgm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import git.soulbgm.mapper.SysConfigParamMapper;
import git.soulbgm.pojo.SysConfigParam;
import git.soulbgm.pojo.vo.SysConfigParamVo;
import git.soulbgm.service.SysConfigParamService;
import git.soulbgm.utils.StringUtil;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static git.soulbgm.common.MonitorConstants.*;

/**
 * 系统配置参数服务实现类
 *
 * @author SoulBGM
 * @date 2022/07/08 18:11
 */
@Service
public class SysConfigParamServiceImpl extends ServiceImpl<SysConfigParamMapper, SysConfigParam> implements SysConfigParamService {

    @Override
    public List<SysConfigParamVo> findByCode(Long code) {
        List<SysConfigParam> list = list(new LambdaQueryWrapper<SysConfigParam>().eq(SysConfigParam::getCode, code));
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        return composeData(list);
    }

    @Override
    public List<SysConfigParamVo> composeData(List<SysConfigParam> list) {
        Map<Long, SysConfigParamVo> map = list.stream().map(SysConfigParamVo::create).collect(Collectors.toMap(SysConfigParamVo::getId, Function.identity()));
        Collection<SysConfigParamVo> dtoList = map.values();
        for (SysConfigParamVo p : dtoList) {
            final Long parentId = p.getParentId();
            if (parentId != 0) {
                map.get(parentId).getChild().add(p);
            }
        }
        return dtoList.stream().filter(o -> o.getParentId() == 0).collect(Collectors.toList());
    }

    @Override
    public String getJson(List<SysConfigParamVo> paramList, int paramType) {
        StringBuilder content = new StringBuilder(paramType == SYS_CONFIG_PARAM_LIST ? "[" : "{");
        int size = paramList.size();
        int maxIndex = size - 1;
        for (int i = 0; i < size; i++) {
            SysConfigParamVo d = paramList.get(i);
            Integer type = d.getType();
            if (type == SYS_CONFIG_PARAM_VALUE) {
                if (paramType == SYS_CONFIG_PARAM_LIST) {
                    content.append(StringUtil.format("\"{}\"{}", d.getValue(), i == maxIndex ? "" : ","));
                } else {
                    content.append(StringUtil.format("\"{}\":\"{}\"{}", d.getName(), d.getValue(), i == maxIndex ? "" : ","));
                }
            } else {
                boolean flag = (type == SYS_CONFIG_PARAM_MAP && paramType == SYS_CONFIG_PARAM_MAP)
                        || type == SYS_CONFIG_PARAM_LIST;
                if (flag) {
                    content.append("\"");
                    content.append(d.getName());
                    content.append("\":");
                }
                content.append(getJson(d.getChild(), type));
                if (i != maxIndex) {
                    content.append(",");
                }
            }
        }
        content.append(paramType == SYS_CONFIG_PARAM_LIST ? "]" : "}");
        return content.toString();
    }

    @Override
    public boolean save(List<SysConfigParamVo> param, Long code) {
        List<SysConfigParam> list = convert(param, code, 0L);
        return saveBatch(list);
    }

    /**
     * 转换
     *
     * @param param    系统配置参数
     * @param code     系统配置信息ID
     * @param parentId 父ID
     * @return {@link List}<{@link SysConfigParam}>
     */
    private List<SysConfigParam> convert(List<SysConfigParamVo> param, Long code, Long parentId) {
        List<SysConfigParam> list = new ArrayList<>(param.size());
        for (SysConfigParamVo v : param) {
            SysConfigParam p = new SysConfigParam();
            long id = IdWorker.getId();
            p.setId(id);
            p.setCode(code);
            p.setName(v.getName());
            p.setValue(v.getValue());
            p.setType(v.getType());
            p.setParentId(parentId);
            list.add(p);
            List<SysConfigParamVo> child = v.getChild();
            if (child != null && !child.isEmpty()) {
                list.addAll(convert(child, code, id));
            }
        }
        return list;
    }

}
